package com.hnkywl.sts.dao.sys;

import com.hnkywl.sts.entity.base.WebserviceLog;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.*;

/**
 * 接口日志查询dao
 * User: ccr
 * Date: 13-9-2
 * Time: 上午10:41
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class WebServiceLogQueryDao extends SimpleHibernateTemplate<WebserviceLog, Long> {

    public Page<WebserviceLog> findLog(String clazz, String method, String type, String paramData, String resultData, String exception, Date datetimeStart, Date datetimeEnd, int pageNo, int pageSize) {
        Page<WebserviceLog> page = new Page<WebserviceLog>(pageNo, pageSize, 0);
        List<String> condition = new ArrayList<String>();
        ArrayList param = new ArrayList();
        String from = " from WebserviceLog ";
        String count = "select count(id)";
        if (StringUtils.isNotEmpty(clazz)) {
            condition.add(" clazz = ? ");
            param.add(clazz);
        }
        if (StringUtils.isNotEmpty(method)) {
            condition.add(" method = ? ");
            param.add(method);
        }
        if (StringUtils.isNotEmpty(type)) {
            condition.add(" type = ? ");
            param.add(type);
        }
        if (StringUtils.isNotEmpty(paramData)) {
            condition.add(" parameterData like ? ");
            param.add("%" + paramData + "%");
        }
        if (StringUtils.isNotEmpty(resultData)) {
            condition.add(" returnData like ? ");
            param.add("%" + resultData + "%");
        }
        if (StringUtils.isNotEmpty(exception)) {
            condition.add(" exception like ? ");
            param.add("%" + exception + "%");
        }
        if (datetimeStart != null) {
            condition.add(" time >= ? ");
            param.add(datetimeStart);
        }
        if (datetimeEnd != null) {
            condition.add(" time <= ? ");
            param.add(datetimeEnd);
        }
        String where = condition.isEmpty() ? "" : (" where " + StringUtils.join(condition, " and "));
        String orderBy = " order by time desc";
        String countHql = count + from + where;
        String resultHql = from + where + orderBy;
        Long total = (Long) createQuery(countHql, param.toArray()).uniqueResult();
        page.setTotalCount(total.intValue());
        if (total.intValue() != 0) {
            List<WebserviceLog> list = createQuery(resultHql,param.toArray())
                    .setFirstResult((pageNo - 1) * pageSize)
                    .setMaxResults(pageSize)
                    .list();
            page.setResult(list);
        } else {
            page.setResult(new ArrayList<WebserviceLog>());
        }
        return page;
    }

    public String findDetail(String filedName, Long id) {
        String hql = "select " + filedName + " from WebserviceLog where id = ?";
        String result = (String) createQuery(hql, id).uniqueResult();
        return result;
    }
}
